let app = getApp();
let that = null;
import {
	get
} from "@/common/js/http.js";
import faIcon from "@/components/kilvn-fa-icon/fa-icon.vue"
import confirm from "@/components/confirm/confirm.vue"
export default {
	components: {
		faIcon,
		confirm
	},
	data() {
		return {
			info: [],
			isLoad: false,
			categoryList: [],
			checkAppIsAudit: true,
			isIosVirtualPay: true,
			showActivateModal: false,
			activateCode: '',
			showOnlineServiceNotice: false
		};
	},
	onLoad(options) {
		that = this;
		that.getInfo(options.id);
	},
	onShow(options) {
		app.globalData.showShareMenu();
		that.checkAppIsAudit = app.globalData.checkAppIsAudit();
		that.isIosVirtualPay = app.globalData.checkIsIosVirtualPay();
	},
	onShareAppMessage() {
		let info = that.info;
		let config = app.globalData.getShareConfig();
		config.title = info.name;
		config.path = '/pages/search/course/detail?id=' + info.id;
		return config;
	},
	methods: {
		async getInfo(id) {
			app.globalData.server
				.getRequest('course/getInfo', {
					id: id
				})
				.then(function(res) {
					let item = res.data;
					that.setData({
						info: item,
						isLoad: true
					});
				})
				.catch(function(res) {
					app.showToast('获取信息失败');
				});
			const categoryRes = await get('paper/category/list', {
				course_id: id,
				level: 1
			});
			that.categoryList = categoryRes.data;

		},
		onLearn(options) {
			let	url = '/pages/practice/material/list?title=' + that.info.name + '&id=' + that.info.id
			uni.navigateTo({
				url: url
			});
		},
		async onQuestionTypePractice() {
			const countInfo = await get('question/getCount', {
				course_id: that.info.id,
			});
			const countInfoData = countInfo.data;
			if (countInfoData.count <= 0) {
				app.showToast('题库未上传题目');
				return;
			}
			let url = '../topic/topic?mainType=0&title=' + that.info.name + '&code=' + that.info.code + '&id=' +
				that.info.id + '&chargeType=1';
			uni.navigateTo({
				url: url
			});
		},
		async onOrderPractice() {
			const countInfo = await get('question/getCount', {
				course_id: that.info.id,
			});
			const countInfoData = countInfo.data;
			if (countInfoData.count <= 0) {
				app.showToast('题库未上传题目');
				return;
			}
			let url =
				'../practice?title=' +
				that.info.name +
				'&mainType=0' +
				'&id=' +
				that.info.id +
				'&max_page=' + countInfoData.max_page;
			uni.navigateTo({
				url: url
			});
		},
		onCategory(cate) {
			console.log(cate);
			let url = '';
			if (cate.is_multi_level) {
				// 多级分类则进入章节题目
				url = '/pages/practice/paper/category?title=' + cate.name + '&category_id=' + cate.id + '&course_id=' +
					that.info.id;
			} else {
				// 单层级直接进入试卷中心
				url = '/pages/practice/paper/paper?title=' + cate.name + '&category_id=' + cate.id
			}
			uni.navigateTo({
				url: url
			});
		},
		onCollect() {
			app.globalData.server
				.postRequest('v2/joinCourses', {
					course_id: that.info.id,
					join: that.info.collect != 1 ? 0 : 1
				})
				.then(function(e) {
					let info = that.info;
					info.collect = info.collect == 1 ? 2 : 1;
					that.setData({
						info: info,
					});
				})
				.catch(function(e) {
					app.showToast('收藏失败');
				});
		},
		onSearchQuestion() {
			let url = '/pages/search/list?selectModelIndex=1&course_id=' + that.info.id;
			console.log(url);
			uni.navigateTo({
				url: url
			});
		},
		onErrorQuestion() {
			let url = '/pages/practice/error/error?course_id=' + that.info.id;
			uni.navigateTo({
				url: url
			});
		},
		onBuyCourse() {
			let url = '/pages/practice/course/buy?id=' + that.info.id;
			uni.navigateTo({
				url: url
			});
		},
		onColl() {
			let url = '/pages/practice/coll/coll?course_id=' + that.info.id;
			uni.navigateTo({
				url: url
			});
		},
		onNote() {
			app.showToast('敬请期待');
			return;
		},
		showOnlineServiceTap() {
			that.showOnlineServiceNotice = true;
		},

		activateVIP() {
			if (!that.activateCode) {
				app.showToast('请输入激活码');
				return;
			}
			
			// 这里添加激活码验证逻辑
			app.globalData.server
				.postRequest('v2/activateCourse', {
					course_id: that.info.id,
					activate_code: that.activateCode
				})
				.then(function(res) {
					app.showToast('激活成功');
					that.showActivateModal = false;
					that.activateCode = '';
					// 刷新课程信息
					that.getInfo(that.info.id);
				})
				.catch(function(err) {
					app.showToast(err.message || '激活失败');
				});
		}
	}
};